// Copyright  2012 Petr TALLA. [petr.talla@gmail.com]
// This file is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License
// as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This file is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY;  without even the implied warranty
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU Lesser General Public License for more details.
// [http://www.gnu.org/copyleft/lesser.html]
//=============================================================================

package tc.crase.cmds;

import java.awt.Color;
import java.awt.Point;

import javax.swing.JList;
import javax.swing.JTextField;

import tc.common.registry.RegistrySys;
import tc.common.utility.GrView;
import tc.common.utility.TcDisplayXY;
import tc.common.utility.Vector2D;
import tc.crase.db.Xattr;
import tc.crase.db.XattrGRP;
import tc.crase.db.Xobject;
import tc.crase.sys.XdbRoot;
import tc.crase.sys.XobjectEx;
import tc.crase.view.GrDocGraph;
import tc.crase.view.ViewGraph;
import tc.crase.view.ViewRoot;

public class CmdDocs extends CmdEx {
	public CmdDocs() {
	}
	
	JList list;
	JTextField renameText;
	
	public void initializeDialog() {
		super.initializeDialog();
		setLabel("Open Graph");
		setHint("Select object containing graph. Next selection opens the graph.");
		
		finalizeDialog();
		
		Selection.objectsAllowed = true;
	}
	
	public void enterSelected(Point pt, GrView view, Object object) {
		XattrGRP attrGraph = null;
		
		Out: for (Object obj : Selection.selset)
		{	XobjectEx xobj = (XobjectEx)obj;
			
			for (Xattr attr : xobj) {
				XattrGRP attrGrp = attr.getAsGRP();
				if (attrGrp == null) continue;
				attrGraph = attrGrp;
				break Out;
			}
		}
		
		if (attrGraph != null) {
			ViewGraph viewGraph = new ViewGraph(attrGraph);
			viewGraph.setBackground(Color.GRAY);
			viewGraph.transf.scaleX = 1.0;
			viewGraph.transf.scaleY = 1.0;
			viewGraph.transf.width  = 0.1;
			viewGraph.transf.height = 0.1;
			viewGraph.type = GrView.Type.GRAPH;
			
			ViewRoot.createFrame(viewGraph, "Graph: " + attrGraph.parent.getObjectName());
		}
		
		RegistrySys.registry.lock(); {
			Selection.clean();
		
			if ( (attrGraph == null) && (object instanceof XobjectEx ) ) {
				Selection.add((XobjectEx)object);
			}
		} RegistrySys.registry.unlock();

	}
	
	public void enterReset(GrView view) {
		RegistrySys.registry.lock(); {
			Selection.clean();
		} RegistrySys.registry.unlock();
	}
	
	public void enterMove(Point pt, GrView view)  {
	}
}
